clear
eststo clear
// Using Ranking Data
use ../dta/index.dta, clear
keep if abs(running)<=0.5
set scheme stmono1
//Merging on Returns and Industry Codes
merge 1:1 code rd_group using ../dta/daily_returns_rd.dta, nogen keep(match master)
merge m:1 code using ../src/industry, nogen keep(match master)

// Generating Dummies
tab rd_group, gen(dum_rd_group)
tab industry, gen(dum_3ind)
		
// Generating Binned Means		
gen running_1=floor(running*20)
gen midpoint=(running_1+0.5)/20
br code running* midpoint
foreach var in car_ret5 car_ret20 car_ret60 D margin_hold60 margin_to_cap60{
	bys running_1: egen bin_`var'=mean(`var') 
}
		
// Panel A		
twoway (lpolyci D running if running<-0.001  , deg(1) lpattern(solid) clcolor(black) fcolor(gray%20) lcolor(white)) ///
		 (lpolyci D running if running>0.001 , deg(1) lpattern(solid) clcolor(black) fcolor(gray%20) lcolor(white)) ///
		  (scatter bin_D midpoint, msymbol(circle_hollow) mcolor(black))	,  xline(0, lcolor(red)  lpattern(solid)) legend(off)
graph export ../gph/rd_plot_D.pdf, replace

// Panel B
twoway (lpolyci margin_hold60 running if running<-0.0001  , deg(1) lpattern(solid) clcolor(black) fcolor(gray%20) lcolor(white)) ///
		 (lpolyci margin_hold60 running if running>0.0001 , deg(1) lpattern(solid) clcolor(black) fcolor(gray%20) lcolor(white)) ///
		  (scatter bin_margin_hold60 midpoint, msymbol(circle_hollow) mcolor(black)), xline(0, lcolor(red)  lpattern(solid))	legend(off)
graph export ../gph/rd_plot_margin_hold60.pdf, replace

// Panel C	
twoway (lpolyci margin_to_cap60 running if running<-0.0001  , deg(1) lpattern(solid) clcolor(black) fcolor(gray%20) lcolor(white)) ///
		 (lpolyci margin_to_cap60 running if running>0.0001 , deg(1) lpattern(solid) clcolor(black) fcolor(gray%20) lcolor(white)) ///
		  (scatter bin_margin_to_cap60 midpoint, msymbol(circle_hollow) mcolor(black)), xline(0, lcolor(red)  lpattern(solid))	 ylabel(0(0.02)0.06) legend(off)
graph export ../gph/rd_plot_margin_cap60.pdf, replace
		
			
// Panels D-F				  
foreach x in 5 20 60{
	twoway (lpolyci car_ret`x' running if running<-0.001  , deg(1) lpattern(solid) clcolor(black) fcolor(gray%20) lcolor(white)) ///
			 (lpolyci car_ret`x' running if running>0.001 , deg(1) lpattern(solid) clcolor(black) fcolor(gray%20) lcolor(white)) ///
			  (scatter bin_car_ret`x' midpoint, msymbol(circle_hollow) mcolor(black))	, ylabel(-0.1(0.1)0.2)	  xline(0, lcolor(red) lpattern(solid)) legend(off)
	graph export ../gph/rd_plot_car_ret`x'.pdf, replace
}
						  

